<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <div class="layui-col-sm3 layui-col-md3 layui-col-lg3 layui-col-md-offset1">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-inline">
                <input id="input_title" type="text" name="title" lay-verify="required" placeholder="请输入标题"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-col-sm3 layui-col-md3 layui-col-lg3">
            <label class="layui-form-label">文章作者</label>
            <div class="layui-input-inline">
                <input id="username" type="text" name="author" lay-verify="required" placeholder="请输入姓名"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-col-sm3 layui-col-md3 layui-col-lg3">
            <div class="layui-btn-container layadmin-layer-demo">
                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="layuiadmin-app-form-submit"
                        id="layuiadmin-app-form-submit" data-type="test1">添加
                </button>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-col-sm3 layui-col-md3 layui-col-lg3 layui-col-md-offset1">
            <label class="layui-form-label">发布时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="test-laydate-type-datetime" placeholder="直接点击选现在">
            </div>
        </div>
        <div class="layui-col-sm3 layui-col-md3 layui-col-lg3">
            <label class="layui-form-label">所属分类</label>
            <div class="layui-input-inline" id="option_select_input">
                <select id="selectId" lay-filter="classify">
                </select>
            </div>
        </div>
        <div class="layui-col-sm3 layui-col-md3 layui-col-lg3">
            <div class="layui-btn-container layadmin-layer-demo">
                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="layuiadmin-app-form-submit1"
                        id="layuiadmin-app-form-submit1" data-type="test11">关闭
                </button>
            </div>
        </div>
    </div>
</div>
<div class="layui-form-item">
    <label class="layui-form-label"></label>
    <div class="layui-input-inline">
        <textarea id="textarea_id" name="content" style="width:930px;height:600px;"
                  class="layui-textarea"></textarea>
    </div>
</div>

<link rel="stylesheet" href="/static/kindeditor-master/themes/simple/simple1.css" />
<script charset="utf-8" src="/static/kindeditor-master/kindeditor-all.js"></script>
<script charset="utf-8" src="/static/kindeditor-master/lang/zh-CN.js"></script>
<script charset="utf-8" src="/static/kindeditor-master/plugins/code/prettify.js"></script>
<script>

    console.log('12345')

    var editor = KindEditor.create(('#textarea_id'), {
        //参数配置
        //readonlyMode : true, // 设置成只读
        width: '930px',
        height: '600px',
        allowFileManager: true, //是否允许浏览服务器已上传文件,默认是false
        filePostName: "file",
        uploadJson: '/fileupload/uploadArticleImg', // 是富文本里面上传图片的接口
        minHeight: '600',
        resizeType: 0,//禁止拉伸，1可以上下拉伸，2上下左右拉伸
        filterMode: false,//true时过滤HTML代码，false时允许输入任何代码。
        itmes: [
            'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
            'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
            'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
            'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
            'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
            'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
            'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
            'anchor', 'link', 'unlink', '|', 'about'
        ],
    })


    layui.use(['admin', 'form', 'laydate'], function () {
        var laydate = layui.laydate;
        var $ = layui.$
            , admin = layui.admin
            , element = layui.element
            , form = layui.form
            , router = layui.router();

        console.log('11111111')
        //日期时间选择器
        laydate.render({
            elem: '#test-laydate-type-datetime'
            , type: 'datetime'
            , format: 'y.MM.dd-HH:mm'
            , done: function (date) {
                console.log(date); //得到日期生成的值，如：2022-03-01
            }
        });

        console.log('22222')
        $.ajax({
            url: '/acquire_list/',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                console.log(data)   //data[data]表示所有的选项
                $.each(data["data"], function (index, item) {
                    console.log(new Option(item));
                    $('#selectId').append(new Option(item));//往下拉菜单里添加元素
                })
                form.render('select'); //刷新表单select选择框渲染
            }
        });
        // 1.传过来的data是一个大字典，data["data"]表示data字典里面的data那段列表数据.
        // 2.selectId表示对应的选择框，append表示往选择框里面添加选项
        // 3.each表示循环，index表示有几次循环，就遍历几次比如 0:电子，1:机械。
        // 4.item表示遍历data["data"]的数据


        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function (data) {
            console.log(data)
            var field = data.field; //获取提交的字段,这里的field输出的是JS格式的内容传到后端
            var html = editor.html(); //监听获取文本编辑器里面的内容
            var publish_time_front = $('#test-laydate-type-datetime').val(); //监听获取发布时间框内的内容
            var classify = $('#selectId').val();


            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                url: "/create_article/",
                type: 'post',
                datatype: "json",
                data: {
                    field_back: field,
                    content_back: html,
                    publish_time_back: publish_time_front,
                    classify_back: classify
                },
                success: function (res) {
                    console.log(res)
                }
            });
            //$layui.req()
        });
        var active = {
            test1: function () {
                layer.confirm('文章已提交', {
                    btn: ['返回文章', '返回目录'] //按钮
                }, function () {
                    window.location.href = "#/cpa/homepage";
                }, function () {
                    window.location.href = "/static/layuiAdmin-master/admin/std/dist/views/app/content/list1.html";
                });
            }
        }

        $('#layuiadmin-app-form-submit').on('click', function () {
            var type = $(this).data('type');
            active[type] && active[type].call(this);
        })
    })
    ;

</script>
